package com.hiyiin.IntelligentHospital.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hiyiin.IntelligentHospital.common.result.Result;
import com.hiyiin.IntelligentHospital.common.util.MD5;
import com.hiyiin.IntelligentHospital.hosp.service.HospitalSetService;
import com.hiyiin.IntelligentHospital.model.hosp.HospitalSet;
import com.hiyiin.IntelligentHospital.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//加上该注解允许跨域访问
//@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    //1查询医院设置表中的所有信息
    @ApiOperation(value = "获取全部医院设置信息")
    @GetMapping
    public Result getAllHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    //2逻辑删除医院设置
    @ApiOperation(value = "逻辑删除医院设置信息")
    @DeleteMapping("/{id}")
    public Result removeHospitalSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }

    //3条件查询(带分页)
    @ApiOperation(value = "条件查询医院设置信息(带分页)")
    @GetMapping("/{currentPage}/{pageSize}")
    public Result getPage(@PathVariable Long currentPage,
                          @PathVariable Long pageSize,
                          HospitalSetQueryVo hospitalSetQueryVo){
        IPage<HospitalSet> page = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<HospitalSet> lqw = new LambdaQueryWrapper<>();

        lqw.like(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname()),HospitalSet::getHosname,hospitalSetQueryVo.getHosname());
        lqw.eq(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode()),HospitalSet::getHoscode,hospitalSetQueryVo.getHoscode());

        return Result.ok(hospitalSetService.page(page,lqw));
    }

    //4添加医院设置
    @ApiOperation(value = "添加医院设置信息")
    @PostMapping
    public Result addHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态：1可用 0不可用
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + ""+ random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        return save?Result.ok():Result.fail();
    }

    //5根据id获取医院设置
    @ApiOperation(value = "根据id获取医院设置信息")
    @GetMapping("/{id}")
    public Result getHospitalSetById(@PathVariable Long id){
        //模拟异常
        //try {
        //    int a = 1/0;
        //} catch (Exception e) {
        //    throw new IntelligentHospitalException("失败",201);
        //}
        return Result.ok(hospitalSetService.getById(id));
    }

    //6修改医院设置
    @ApiOperation(value = "修改医院设置信息")
    @PutMapping
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean update = hospitalSetService.updateById(hospitalSet);
        return update?Result.ok() : Result.fail();
    }

    //7批量删除医院设置
    @ApiOperation(value = "批量删除医院设置信息")
    @DeleteMapping
    public Result batchRemoveHospitalSet(@RequestBody List<Long> ids) {
        hospitalSetService.removeByIds(ids);
        return Result.ok();
    }

    //8医院接口的锁定和解锁
    @ApiOperation(value = "医院接口的锁定和解锁")
    @PutMapping("/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        //根据id查询医院设置信息
        HospitalSet hospitalSet = this.hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //修改数据库信息
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    //9发送签名密钥
    @ApiOperation(value = "发送签名密钥")
    @PutMapping("/{id}")
    public Result sendKey(@PathVariable Long id){
        //根据id查询医院设置信息
        HospitalSet hospitalSet = this.hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        // TODO 发送短信
        return Result.ok();
    }
}
